import { addEntryMaterial } from "@/api/material-management/material-entry";
import { supplierList } from "@/api/material-management/supplier";
import { materialType, entryType } from "../constant/index.js";
export const mixins = {
  data() {
    const storageNumber = (rule, value, callback) => {
      if (value > 0) {
        callback();
      } else {
        return callback(new Error("入库数量必须大于0"));
      }
    };
    return {
      materialType,
      entryType,
      materialDialog: false,
      materialRules: {
        materialId: [
          { required: true, message: "请选择物料", trigger: "change" },
        ],
        storageType: [
          { required: true, message: "请选择物料类型", trigger: "change" },
        ],
        storageNumber: [
          { required: true, validator: storageNumber, trigger: "blur" },
        ],
        batchNumber: [
          { required: true, message: "请输入物料批次", trigger: "blur" },
        ],
        storageDate: [
          { required: true, message: "请选择日期", trigger: "change" },
        ],
        dateOfManufacture: [
          { required: true, message: "请选择日期", trigger: "change" },
        ],
        supplierId: [
          { required: true, message: "请选择供应商", trigger: "change" },
        ],
      },
    };
  },
  created() {
    this.getsupplierList();
    this.materialForm.batchNumber = this.defaultBatchNumber;
  },
  methods: {
    //供应商
    async getsupplierList() {
      let res = await supplierList({
        area: [],
        detailAddress: "",
        address: "",
        contactPeople: "",
        id: null,
        name: "",
        pageNum: 0,
        pageSize: 10,
        phoneNumber: "",
        remark: "",
        state: "",
      });
      if (res.code == 200) {
        this.supplierList = res.data.records;
      }
    },
    confirm() {
      this.$refs.materialForm.validate(async (valid) => {
        if (valid) {
          let res = await addEntryMaterial(this.materialForm);
          if (res.code == 200) {
            this.materialDialog = false;
            this.getTableData();
          }
        } else {
          return false;
        }
      });
    },
    //分页器
    handlePage(val) {
      this.formData.pageNum = val.page;
      this.formData.pageSize = val.limit;
      this.getTableData();
    },
  },
  computed: {
    defaultBatchNumber() {
      const nowTime = new Date();
      let month = nowTime.getMonth() + 1;
      let day = nowTime.getDate();
      month < 10 ? (month = `0${month}`) : month;
      day < 10 ? (day = `0${day}`) : day;
      return `${nowTime.getFullYear()}${month}${day}`;
    },
  },
};
